﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Collections.Generic;

public class WriteEnumMenu : MonoBehaviour
{
	[MenuItem("Paperclips/Enums/Write Scene Enum...")]
	public static void WriteSceneEnum()
	{
		List<string> scenes = new List<string>();
		foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
		{
			string path = scene.path;
			path = path.Remove(0, path.LastIndexOf("/") + 1).Replace(".unity", "");
			scenes.Add(path);
		}

		WriteEnum.UpdateEnum("/Scripts/Managers/", "SceneIDs", scenes, "SceneID");
	}

	[MenuItem("Paperclips/Enums/Write Screen Enum...")]
	public static void WriteScreenEnum()
	{
		List<string> elements;
		
		elements = Resources.LoadAll<GameObject>("UI/Screens").Select(x => x.name).ToList();
		WriteEnum.RewriteEnum("/Scripts/Managers/Screens/", "ScreenIDs", elements, "ScreenID");

		elements = Resources.LoadAll<GameObject>("UI/Popups").Select(x => x.name).ToList();
		WriteEnum.RewriteEnum("/Scripts/Managers/Screens/", "ScreenIDs", elements, "PopupID");

		elements = Resources.LoadAll<GameObject>("UI/Panels").Select(x => x.name).ToList();
		WriteEnum.RewriteEnum("/Scripts/Managers/Screens/", "ScreenIDs", elements, "PanelID");

		elements = Resources.LoadAll<GameObject>("UI/Widgets").Select(x => x.name).ToList();
		WriteEnum.RewriteEnum("/Scripts/Managers/Screens/", "ScreenIDs", elements, "WidgetID");

		elements = Resources.LoadAll<GameObject>("UI/Buttons").Select(x => x.name).ToList();
		WriteEnum.RewriteEnum("/Scripts/Managers/Screens/", "ScreenIDs", elements, "ButtonID");
	}

	[MenuItem("Paperclips/Enums/Append Door Enum...")]
	public static void AppendDoorEnum()
	{
		Door[] doors = GameObject.FindObjectsOfType<Door>() as Door[];
		string[] items = new string[doors.Length];
		string sceneName = EditorApplication.currentScene;
		sceneName = sceneName.Remove(0, sceneName.LastIndexOf("/") + 1).Replace(".unity", "");
		for (int i = 0; i < doors.Length; i++)
		{
			string nb = NumberToText(i);
			items[i] = sceneName + "_" + nb;
		}
		
		WriteEnum.AppendEnum("/Scripts/Interactives/Door/", "DoorIDs", new List<string>(items), "DoorID");
	}

	private static string NumberToText(int _nb)
	{
		int n1 = _nb % 26;
		int n2 = _nb / 26;
		return (char)(n2 + 'A') + "" + (char)(n1 + 'A');
	}
}
